package com.daykw.douyin.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.http.HttpBase;
import com.daykw.douyin.model.VideoAuth;
import com.daykw.douyin.utils.SpringContextHolder;

public class VideoZBSearchThread extends Thread {

	private static Logger logger = Logger.getLogger(VideoZBSearchThread.class);
	private NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

	@Override
	public void run() {

		HttpBase http = new HttpBase();
		Map<String, String> headers = new HashMap<String, String>();
		Record c = dao.fetch("t_cookie", Cnd.where("name", "=", "13235801201"));
		if (c == null) {

			logger.info("cookie:13235801201没了...");
			return;
		}
		String cookie = c.getString("cookie");
		headers.put("Cookie", cookie);

		for (int i = 1; i <= 50; i++) {
			try {
				String surl = "https://dy.feigua.cn/Blogger/Search?keyword=&likes=0&fans=0&tag=%E7%BE%8E%E5%A6%86&gender=0&age=0&province=0&score=0&city=0&sort=0&mainfansGender=0&mainfansAge=0&mainfansProvince=0&mainfansCity=0&shares=0&comments=0&isWithCommerceEntry=undefined&isCustomVerify=undefined&isEnterpriseVerify=undefined&verify=&hasgoods=0&page="
						+ i + "&partial=0";

				String html = http.get(surl, headers);

				if (html.indexOf("立即升级会员版本") > 0) {
					break;
				} else {

					String dataid = null;
					String auth_url = null;
					Document doc = Jsoup.parse(html);
					Elements awemelist = doc.select("div.v-search-item ");

					for (Element tr : awemelist) {

						Element u = tr.select("ul.v-zs-btns a").first();
						String href = u.attr("href");
						href = href.replace("#", "");
						dataid = href.substring(href.indexOf("=") + 1, href.indexOf("&"));
						auth_url = "https://dy.feigua.cn" + href;
						String nickname = tr.select("span.nickname").text();
						String img = tr.select("dt img").attr("src");

						this.saveData(dataid, auth_url, img, nickname);

					}

					logger.info("#########列表翻页第" + i + "页");

					Thread.sleep(5000);
				}
			} catch (Exception e) {

				logger.error("", e);
			}
		}

	}

	private void saveData(String dataid, String auth_url, String img, String nickname) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		VideoAuth auth = dao.fetch(VideoAuth.class, Cnd.where("dataid", "=", dataid));
		if (auth == null) {
			auth = new VideoAuth();
			auth.setDataid(dataid);
			auth.setAuth_url(auth_url);
			auth.setStatus(0);
			auth.setKw_nick_name(nickname);
			auth.setVideo_status(0);
			auth.setCreate_time(new Date());
			auth.setCdate(Integer.parseInt(fmt.format(new Date())));
			auth.setSearch("search");
			try {
				dao.fastInsert(auth);
			} catch (Exception e) {
				// logger.error("", e);
				logger.error("dataid重复:" + dataid);
			}
		} else {
			dao.update(VideoAuth.class, Chain.make("search", "search"), Cnd.where("dataid", "=", dataid));
		}

		logger.info("#########完成高级用户搜索：" + dataid);

	}
}
